<?php
namespace app\admin\controller;
use think\Db;
use third\Data;

class Menu extends Base{
              /*
               * 菜单列表
               */
    public function index(){
        $list = Db::name('menu')->order('list_order asc, menu_id asc')->select();
        $list =  Data::tree($list, 'menu_name', 'menu_id', 'parent_id');
        $this->assign('list', $list);
        return $this->fetch();
    }
     /*
      * 添加菜单
      */
     public function addMenu(){
         if(IS_POST){
              $menu = I('post.');
              if($menu['parent_id'] > 0 ){
                  $level = Db::name('menu')->field('menu_level')
                                           ->where(array('menu_id' => $menu['parent_id']))
                                           ->find();

                  $menu['menu_level'] = (int)$level['menu_level'] + 1;
              }
               $result = Db::name('menu')->insert($menu);

               $data = $result ? array('status' => 1, 'msg' => '添加成功')
                               : array('status' => 0, 'msg' => '添加失败');

                          ajaxReturn($data);
         }else{
             $list = Db::name('menu')->order('list_order desc, menu_id asc')->select();
             $list =  Data::tree($list, 'menu_name', 'menu_id', 'parent_id');
             $this->assign('list', $list);//菜单选项
             return $this->fetch('add_menu');
         }
 }

  /*
   *修改菜单
   */
  public function editMenu(){
      if(IS_AJAX){
          $menu_id = I('get.menu_id');
          $menu = I('post.');
          if($menu ['parent_id'] == $menu_id) $this->error("不能将自身作为上级菜单");
          $result =  Db::name('menu')->where(array('menu_id' => $menu_id))->update($menu);

          $data = $result ? array('status' => 1, 'msg' => '修改成功')
                          : array('status' => 0, 'msg' => '修改失败');

                 ajaxReturn($data);

      }else{
          $menu_id = I('get.menu_id');
          $info = Db::name('menu')->where(array('menu_id' => $menu_id))->find();
          if($info){
              $list = Db::name('menu')->order('menu_id asc')->select();
              $list =  Data::tree($list, 'menu_name', 'menu_id', 'parent_id');
              $this->assign('list', $list);//菜单选项
              $this->assign('info', $info);
              return $this->fetch('edit_menu');
          }else{
              $this->error('该菜单不存在',U('Menu/index'));
          }
      }
  }
   /*
    * 删除菜单
    */
   public function delMenu(){
            if(IS_AJAX){
                $menu_id = I('get.menu_id');
                $count = Db::name('menu')->where(array('parent_id' => $menu_id))->count();
                 if($count > 0){
                     $data = array('status' => 0, 'msg' => '存在子菜单，无法删除！');
                 }else{
                     $result = Db::name('menu')->where(array('menu_id' => $menu_id))->delete();

                     $data = $result ? array('status' => 1, 'msg' => '删除成功')
                                     : array('status' => 0, 'msg' => '删除失败');
                 }
                           ajaxReturn($data);
             }

   }

           /*
            * 菜单排序
            */
    public function orderMenu(){
        if(IS_AJAX){
            $list_order = I('list_order');
            $result = list_order($list_order, 'Menu', 'menu_id'); //列表排序
            $data = $result ? array('status' => 1, 'msg' => '排序成功！')
                            : array('status' => 0, 'msg' => '排序失败！');
            return ajaxReturn($data);
        }

    }

        /*
         * 显示或不显示菜单
         */
    public function isShowMenu(){
        if(IS_AJAX) {
            $menu_id = I('get.menu_id');
            $is_show = I('get.is_show');
            $is_show = $is_show  ? 0 : 1;
            $result = Db::name('menu')->where(array('menu_id' => $menu_id))
                                      ->update(array('is_show' => $is_show));

            $data = $result ? array('status' => 1)
                            : array('status' => 0);

            ajaxReturn($data);
        }
    }
}
